package com.example.controller;
import com.example.service.Lesson007Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/*
@description:接口性能调优之大事务优化
@ClassName TransactionController
@author chen
@create 2025-06-24 18:17
@Version 1.0
*/
@RestController
@RequestMapping("transaction")
public class TransactionController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(TransactionController.class);

    @Resource
    private Lesson007Service lesson007Service;

    /*
     * 声明式事务，事务范围比较大
     *
     * @throws InterruptedException
     */
    @GetMapping("/bigTransaction")
    public boolean bigTransaction()
    {
        try
        {
            this.lesson007Service.bigTransaction();
            return true;
        }
        catch (Exception e)
        {
            LOGGER.error("声明式事务 执行异常:{}", e.getMessage());
            return false;
        }
    }

    /*
     * 使用 TransactionTemplate 编程式事务，可以灵活的控制事务的范围
     *
     * @throws InterruptedException
     */
    @GetMapping("/smallTransaction")
    public boolean smallTransaction()
    {
        try
        {
            this.lesson007Service.smallTransaction();
            return true;
        }
        catch (Exception e)
        {
            LOGGER.error("编程式事务 执行异常:{}", e.getMessage());
            return false;
        }
    }
}
